# @Time : 2022/4/12 11:31

import hashlib, json, base64
import requests

from Administrative_office_system.settings import IS_TEST


def md5value(key):
    input_name = hashlib.md5()
    input_name.update(key.encode("utf-8"))
    return (input_name.hexdigest()).lower()


def send_mssage(mobiles, content):
    if IS_TEST:
        return True
    ecName = "京山市人民医院"
    apId = "oapro"
    mobiles = ",".join(mobiles)
    content = content
    sign = "D32WddTwR"
    addSerial = ""

    # 生成md5的mac值，将ecName、apId、secretKey、mobiles、content、sign、addSerial进行无间隔拼接后生成32位的小写md5值
    g_mac = hashlib.md5()
    g_mac_cont = ecName + apId + 'Jsyy@2020' + mobiles + content + \
                 sign + addSerial

    g_mac.update(g_mac_cont.encode('utf-8'))
    content_a = {
        "ecName": ecName,
        "apId": apId,
        "mobiles": mobiles,
        "content": content,
        "sign": sign,
        "addSerial": addSerial,
        "mac": md5value(g_mac_cont),
    }
    # content_a["mac"] = md5value(g_mac_cont)

    # 将content_a转为json字串后进行base64编码加密
    encoding_content = base64.b64encode(json.dumps(content_a).encode('utf-8'))
    strs = str(encoding_content, 'utf-8')
    try:
        print("base64字符串：", g_mac_cont)
    except Exception as E:
        print("编码格式错误：", E)
    # 发送信息，更多接口见后面

    response = requests.post(url="http://112.35.1.155:1992/sms/norsubmit", data=strs,
                             headers={'Content-Type': 'application/json', 'charset': 'UTF-8'})

    # 读取响应
    print(response.json())
    return response.json()


if __name__ == '__main__':
    res = send_mssage(['15978554102', "13972862128", "13972892827", "18972875655", "13581338555", "13972913266", "13677263020", "13986992559", "13971834570", "13797984959", "13997922858", "13627158079", "13597997166", "13597953888", "13797921210", "13886903601", "15872981266", "13774003368", "13477359663", "15926680020", "13657150680", "13451177554", "13677267960", "15071916889", "15972680080", "13972884559", "18672176829", "15571596110", "15071923236", "15623338448", "18271905750", "18399904652", "13986982801"], '总值班：1月份总值班排班已发至您OA邮箱，请查收。')
    print(res)
